<?php 
class GoodsListController extends CommonController{
	    private $model;
		
		public function __init(){
			parent::__init();
			$this->model=K("GoodsList");
		}	
	
		public function index(){
		  	//查找货品表的数据在页面循环******
		  	  //查询货品表
		  	  $gid=Q('get.gid','','intval');
			$list=$this->model->where("goods_gid={$gid}")->all();
			  //把属性id组合 变成数组 压入到$list['combin']
			foreach ($list as $k => $v) {
				$list[$k]['combin']=explode(",", $v['combin']);
				foreach ($list[$k]['combin'] as $kk => $vv) {
				$list[$k]['combin_name'][]=M("goods_attr")->where("gtid={$vv}")->getField("gtvalue");
				}
				
			}	
//			p($list);	
		$this->assign("list",$list);		
		//查找对应的属性		
		
		//商品属性表和类型属性表关联 获取属性名称
		$data=M()->join("__goods_attr__ gt JOIN __shop_typeattr__ ta ON gt.shop_typeattr_taid=ta.taid")->where("goods_gid={$gid} and class=1")->group("taname")->all();
		
		foreach ($data as $k => $v) {
			$data[$k]['gtvalue']=M("goods_attr")->where("goods_gid={$gid} and shop_typeattr_taid={$v['taid']}")->all();					
		}
		
        $this->assign('data',$data);
//		 p($data);
	
		$this->display();	
		
	    }
	
		/**
		 * 添加
		 */
		public function add(){
		 	if(IS_POST){
		 		if(!$this->model->addlist()) $this->error($this->model->error);	 
				$this->success("添加成功");		
		 	}		 	
		}
		/**
		 * 删除
		 */
		public function del(){
			$glid=Q("get.glid","","intval");
			$this->model->where("glid={$glid}")->delete();
			$this->success("删除成功");
		}
		/**
		 * 编辑
		 */
		 public function edit(){
		 
			
		 	if(IS_POST){
		 	 if(!$this->model->editlist()) $this->error($this->model->error);
				$this->success('编辑成功');
		 		
		 	}
			
			//处理旧数据
			$glid=Q("get.glid","","intval");
			$oldData=$this->model->where("glid={$glid}")->find();
			$oldData['combin']=explode(",", $oldData['combin']);
			foreach ($oldData['combin'] as $k => $v) {
				$oldData['combin_name'][]=K('Goodsattr')->where("gtid={$v}")->getField("gtvalue");				
			}
			$this->assign('oldData',$oldData);			
			$this->display();
		 }
	
	
}




 ?>